import websocket
import threading
import time
# ip address and port
# server_address = "wss://127.0.0.1:8443"
server_address = "ws://127.0.0.1:8443"
# subprotocol list
subprotocols = ["wss-b"]
def on_message(ws, message):
	print("Received message: " + message[20:])
	print("processing message...")
	time.sleep(3)
	# process the message
	processed_message = message + " (the message has been processed)"
	print("Sending message: " + processed_message)
    # send the message with the id back to msg-router
	ws.send(processed_message)
def on_error(ws, error):
	print("Error: " + str(error))
def on_close(ws, close_status_code, close_msg):
	print("### closed ###")
def on_open(ws):
	print("### opened ###")
# create the WebSocket client, send the subprotocol at the same time
ws = websocket.WebSocketApp(server_address,
	                            on_open=on_open,
	                            on_message=on_message,
	                            on_error=on_error,
	                            on_close=on_close,
	                            subprotocols=subprotocols)
def run_client():
    while True:
        ws.run_forever()
        print("Trying to reconnect...")
        time.sleep(5)  # wait for 5 seconds before reconnecting

if __name__ == "__main__":
	client_thread = threading.Thread(target=run_client)
	client_thread.start()
